package com.zenjava.update;

import com.zenjava.update.ui.*;
import ensemble.Ensemble2;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class EnsembleWithZenUpdate extends Application
{
    public static void main(String[] args)
    {
        Application.launch(args);
    }

    private StackPane stackPane;
    private ApplicationUpdateWizard applicationUpdateWizard;

    public void start(Stage stage) throws Exception
    {
        Ensemble2 ensemble = new Ensemble2();
        ensemble.start(stage);

        stage.getScene().getStylesheets().add("/zen-update-styles.css");
        stage.getScene().getStylesheets().add("/styles.css");

        BorderPane contentPane = new BorderPane();
        contentPane.setCenter(stage.getScene().getRoot());
        contentPane.setBottom(buildZenUpdateBar());

        this.stackPane = new StackPane();
        this.stackPane.getChildren().add(contentPane);
        stage.getScene().setRoot(this.stackPane);
    }

    private Pane buildZenUpdateBar()
    {
        HBox box = new HBox(20);
        box.setAlignment(Pos.CENTER_RIGHT);
        box.getStyleClass().add("zen-update-bar");

        CurrentVersionBar currentVersionBar = new CurrentVersionBar();
        currentVersionBar.load();
        box.getChildren().add(currentVersionBar);

        Button checkForUpdatesButton = new Button("Check for updates");
        checkForUpdatesButton.setOnAction(new EventHandler<ActionEvent>()
        {
            public void handle(ActionEvent event)
            {
                if (applicationUpdateWizard == null)
                {
                    applicationUpdateWizard = new ApplicationUpdateWizard(stackPane);
                }
                applicationUpdateWizard.show();
            }
        });
        box.getChildren().add(checkForUpdatesButton);

        return box;
    }

}
